
//大类
var item1 = "<div class='item'><div class='item_1'><div class='class_update' style='border: 1px solid #e4e4e4;'><div class='c_name'>{c_name}</div><div class='div_pen'><div class='c_pen'><img class='h_ts' src='img/Edit3.png' /></div></div><div class='right'><div class='c_plus'><img class='h_ts' src='img/add3.png' /></div><div class='c_del'><img class='h_ts' src='img/Delete3.png' /></div></div></div><div class='switch'><div class='close'></div></div></div></div>";

//二类
var item2 = "<div class='div_child'>{item}</div>";

//三类
var item3 = "<div class='item_2'><div class='line'></div><div class='class_update'><div class='c_name'>{c_name}</div><div class='div_pen'><div class='c_pen'><img class='h_ts' src='img/Edit3.png' /></div></div><div class='right'><div class='c_plus'><img class='h_ts' src='img/add3.png' /></div><div class='c_del'><img class='h_ts' src='img/Delete3.png' /></div></div></div></div>";


$(document).ready(function () {

    getData(1, 1);
    getData(1, 3);

    addListener();
});

//绑定事件
function addListener() {

    //显示隐藏
    $(".switch").bind("click", function () {

        var divChild = $(this).parent().next();
        if (divChild.attr("class").indexOf("none") == -1) {
            divChild.addClass("none");

            $($(this).children()).addClass("close");
        }
        else {
            divChild.removeClass("none");
            $($(this).children()).removeClass("close");
        }
    });

    //改变状态
    $(".col4 img").bind("click", function () {

        //产品id
        var id = $($(this).parent().parent()).attr("id");

        var statuscfg = -1;

        var msg = "确定要{statuscfg}该微讯?";

        //判断上架或下架
        if ($.trim($($(this).prev()).text()) == "发送") {
            //当前状态为上架  需要修改为下架
            statuscfg = 3;
            msg = msg.replace(/{statuscfg}/g, "下架");
        }
        else {
            statuscfg = 1;
            msg = msg.replace(/{statuscfg}/g, "上架");
        }

        dialog.confirm("上架/下架", msg, function () {
            chanageState(id, statuscfg);
        }, null);
    });

    //删掉
    $(".del").bind("click", function () {

        //产品id
        var id = $($(this).parent().parent()).attr("id");

        var statuscfg = 2;

        dialog.confirm("操作", "确定要删除该微讯?", function () {
            chanageState(id, statuscfg);
        }, null);
    });

    //编辑
    $(".edit").bind("click", function () {

        //产品id
        var id = $($(this).parent().parent()).attr("id");
        ToUrl("item_add.aspx?id=" + id);
    });


    //改变产品所属
    $(".col3 img").bind("click", function () {

        //产品id
        var id = $($(this).parent().parent()).attr("id");

        var source = 0;

        if ($.trim($(this).attr("src")) == "img/source_1.png") {
            source = 0;
        }
        else if ($.trim($(this).attr("src")) == "img/source_0.png") {
            source = 1;
        }
        else {
            toast.show("参数有误!");
            return;
        }

        var img = $(this);

        ajax.updateSource(id, source, function (json) {

            toast.show(json.msg);
            if (json.code != 0) {
                return;
            }
            $(img).attr("src", "img/source_" + source + ".png");
        });
    });

}

//改变状态
function chanageState(id, statuscfg) {

    ajax.changeState(id, 0, statuscfg, function (json) {
        toast.show(json.msg);
        if (json.code != 0) {

            if (json.code == 2) {
                ToUrl("../login.html");
            }
            return;
        }

        var tr_html = $("<p>").append($("#" + id).clone()).html();
        $("#" + id).remove();

        if (statuscfg == 1) {
            $("#tab1 #tbody1").append(tr_html);
        }
        else if (statuscfg == 3) {
            $("#tab2 #tbody2").append(tr_html);
        }
        else {
            ;
        }

        //上架数量
        var tab1_num = $("#tab1 #tbody1").children().length;
        var tab2_num = $("#tab2 #tbody2").children().length;
        $("#count_3").html(tab2_num);
        $("#count_1").html(tab1_num);
        var total = parseInt($("#count").html());
        if (total > 0)
            total--;
        $("#count").html(total);
    });
}


var pager = new Pager();

var p_html = "<tr id = {id}><td><a href = 'item_add.aspx?id={id}'>{title}</a></td><td>{groupname}</td><td class = 'col3'><img src='img/source_{source}.png'></td><td class='col4'><span>{status_name}</span><img src='img/{status_img}.jpg' /></td><td>{updatedate}</td><td><div class='edit'><img class='edit_ts' src='img/Edit3.png' /></div><div class='del'><img class='del_ts' src='img/Delete3.png' /></div></td></tr>";


//上架产品列表获取
function getProductList(page) {
    var statuscfg = 1;
    if ($("#tab1").attr("class").indexOf("none") == -1) {
        statuscfg = 1;
    }
    else if ($("#tab2").attr("class").indexOf("none") == -1) {
        statuscfg = 3;
    }
    else {
        return;
    }


    if (wait.isShow) {
        return;
    }

    getData(page, statuscfg);
}


//ajax加载数据
function getData(page, statuscfg) {

    var source = 0; //0代表微讯  1代表产品

    var _html = p_html;

    if (statuscfg == 1) {
        _html = _html.replace(/{status_name}/g, "发送").replace(/{status_img}/g, "Sucess");
    }
    else if (statuscfg == 3) {
        _html = _html.replace(/{status_name}/g, "草稿").replace(/{status_img}/g, "Error");
    }
    else {
        return;
    }

    ajax.getProductList(page, source, statuscfg, function (json) {
        if (json.code != 0) {

            //没有数据就不要提示
            if (json.code != 3) {
                toast.show(json.msg);
            }

            if (($("#tab1").attr("class").indexOf("none") == -1 && statuscfg == 1) || ($("#tab2").attr("class").indexOf("none") == -1 && statuscfg == 3)) {
                pager.dismiss();
            }


            return;
        }
        var tbody = null;
        if (statuscfg == 1) {
            tbody = $("#tbody1");
        }
        else {
            tbody = $("#tbody2");
        }

        $(tbody).empty();
        var list = json.item.Table;
        $("#count_" + statuscfg).html(json.item.total); //产品数
        for (var i in list) {
            $(tbody).append(_html.replace(/{id}/g, list[i].id).replace(/{title}/g, list[i].title).replace(/{groupname}/g, list[i].groupname).replace(/{source}/g, list[i].source).replace(/{updatedate}/g, list[i].updatedate));
        }

        if (($("#tab1").attr("class").indexOf("none") == -1 && statuscfg == 1) || ($("#tab2").attr("class").indexOf("none") == -1 && statuscfg == 3)) {
            pager.initPage(page, json.item.total, json.item.pagesize, 5, getProductList);
        }

        addListener();
    });
}







